<?php
//by willson 105328468@qq.com 2015/2/5
class base_model {
	public $db;
	public $tbp;
	public $APP, $app;
	public $ACT, $act;
	public $member_id, $member_name;
	public $smarty;
	public $request;
	public $table; //表名

	public function __construct() {
		global $db;
		global $act, $app;
		global $member_id, $member_name;
		global $table_prefix;
		global $smarty;
		global $request;
		$this->db = $db;
		$this->tbp = $table_prefix;
		$this->APP = $app;
		$this->ACT = $act;
		$this->app = $app;
		$this->act = $act;
		$this->smarty = $smarty;
		$this->request = $request;
		//只能通过SESSION来获取用户的ID？
		$this->member_id = 0;
		$this->member_name = '';
		if (isset($_SESSION['member'])) {
			if (property_exists($_SESSION['member'], 'id')) $this->member_id = $_SESSION['member']->id;
			if (property_exists($_SESSION['member'], 'name')) $this->member_name = $_SESSION['member']->name;
		}
	}

	//查找
	public function find($where, $limit=0, $fields='*') {
		$obj = null;
		if ($where){
			//默认是id
			if (is_int($where)) {
				$sql = "SELECT {$fields} FROM {$this->table} WHERE id='{$where}'";
				$obj = $this->db->get_row($sql);
			//有查询条件
			} else {
				if ($limit==0) {
					$sql = "SELECT {$fields} FROM {$this->table} WHERE {$where}";
					$obj = $this->db->get_results($sql);
				} else if ($limit==1) {
					$sql = "SELECT {$fields} FROM {$this->table} WHERE {$where}";
					$obj = $this->db->get_row($sql);
				} else {
					$sql = "SELECT {$fields} FROM {$this->table} WHERE {$where} LIMIT {$limit}";
					$obj = $this->db->get_results($sql);
				}
			}
		//查找全部
		} else {
			$sql = "SELECT {$fields} FROM {$this->table}";
			$obj = $this->db->get_results($sql);
		}
		return $obj;
	}

	//获取数量
	public function count($where) {
		$count = 0;
		if ($where){
			//默认是id
			if (is_int($where)) {
				$sql = "SELECT count(*) FROM {$this->table} WHERE id='{$where}'";
				$count = $this->db->get_var($sql);
			//有查询条件
			} else {
				$sql = "SELECT count(*) FROM {$this->table} WHERE {$where}";
				$count = $this->db->get_var($sql);
			}
		//查找全部
		} else {
			$sql = "SELECT count(*) FROM {$this->table}";
			$count = $this->db->get_var($sql);
		}
		return $count;
	}

	//判断是否存在
	public function exists($where) {
		return $this->count($where);
	}

	//更新记录
	public function update($where, $data) {
		if (is_numeric($where)) {
			$where = "id='{$where}'";
		}
		$set = '';
		if (is_array($data)) {
			foreach ($data as $key => $value) {
				//处理类如“logins=logins+1”等
				if (is_array($value) && count($value)==2) {
					$value = $value[0].$value[1];
				}
				if ($set) {
					$set .= ", `{$key}`='{$value}'";
				} else {
					$set = "`{$key}`='{$value}'";
				}
			}
		} else {
			$set = $data;
		}
		if ($where) {
			$sql = "UPDATE {$this->table} SET {$set} WHERE {$where}";
		} else {
			$sql = "UPDATE {$this->table} SET {$set}";
		}
		//echo $sql."<br>";
		$this->db->query($sql);
	}

	//新记录
	public function insert($data) {
		$fields = '';
		$values = '';
		if (is_array($data)) {
			foreach ($data as $key => $value) {
				if ($fields) {
					$fields .= ", `{$key}`";
					$values .= ", '{$value}'";
				} else {
					$fields = "`{$key}`";
					$values = "'{$value}'";
				}
			}
		}
		$sql = "INSERT INTO {$this->table} ({$fields}) VALUES ({$values})";
		//echo $sql;
		$this->db->query($sql);
	}

	//删除记录
	public function delete($where) {
		if (is_numeric($where)) {
			$where = "id='{$where}'";
		}
		$sql = "DELETE FROM {$this->table} WHERE {$where}";
		$this->db->query($sql);
	}
}
